<?php
if (isset($_POST["PHPSESSID"])) {
	session_id($_POST["PHPSESSID"]);
}
session_start();
//error_reporting(E_ALL);
class member_controller_cartoon extends SnsController {
	
//	var $right_view = true;//右侧
	/**
	 * 构造函数
	 *
	 * @param unknown_type $app
	 * @param unknown_type $controller
	 */
	public function __construct($app, $controller){
		parent::__construct($app, $controller);
		$this->data = Model::factory('cartoon_model_data');
		$this->chapter = Model::factory('cartoon_model_chapter');
		$this->photo = Model::factory('cartoon_model_photo');
		$this->tag	=	Model::factory('cartoon_model_tag');
		$this->requireLogin();
	}
	
	public function add_form_action(){
		

		$userInfo = friends_util_cache::getUserInfo(self::$login['uid']);

		if($userInfo['flag'] != 1){
			//			$this->_redirect('member', 'login', 'login', array('to'=>($to?$to:"http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'])));
			$this->ret = array('message'=>'您还不是特约用户，暂时不能上传！','url'=>self::_makeURL());
			$this->_renderSingle(self::genRootViewPath('error'));
			//			self::responseJs('您还不是特约用户，暂时不能上传！',self::_makeURL(''));
		}else{
			/**
		 * 标签列表
		 */
			$tag_list = cartoon_util_interface::cacheGetTags();
			/**
		 * 作者列表
		 */
			$authors = cartoon_util_interface::cacheGetAuthors();
			/**
			 * 分类列表
		 	*/
			$catelist	=	cartoon_util_interface::cacheGetCateList();

			$this->ret = array('tags'=>$tag_list,'authors'=>$authors,'catelist' => $catelist);

			$this->view = 'add_cartoon';
		}
	}
	
	/**
	 * 修改
	 *
	 */
	public function edit_form_action(){
		$id = _('id');
		$cartoon = $this->data->getRow($id);
		
		$tag_list = cartoon_util_interface::cacheGetTags();		
		/**
		 * 作者列表
		 */
		$authors = cartoon_util_interface::cacheGetAuthors();
		
		$catelist	=	cartoon_util_interface::cacheGetCateList();
		
		$this->ret = array('tags'=>$tag_list,'authors'=>$authors,'cartoon'=>$cartoon,'catelist' => $catelist);
		
		$this->view = 'add_cartoon';
	}
	/**
	 * 添加漫画
	 *
	 */
	public function add_action(){
		$args['ct_title'] = _('cartoon_name');
		$args['ct_alias_name'] = _('alias_name');
		$args['ct_real_name'] = _('real_name');
		$args['ct_uid']	=	self::$login['uid'];
		$args['ct_author'] = _('selected_author');
		$args['ct_flag'] = _('status',1);
		$args['ct_char'] = cartoon_util_interface::getfirstchar(_('cartoon_name'));		
		$args['ct_py'] = _('cartoon_py');
		$args['ct_nation'] = _('zone')?_('zone'):'1';				
		$args['ct_lang'] = _('language')?_('language'):'1';		
		$args['ct_category'] = _('selected_cate');
		$args['ct_series'] = _('selected_series');	
		$tags	=	$this->parseTag(_('tags'));
		$args['ct_tags']	= 	$tags;
		$args['ct_reader_group']	=	_('readerGroup');
		$args['ct_simple_desc'] = _('s_desc');
		$args['ct_desc'] = _('_cartoon_desc');
		$args['ct_dateline'] = CURRENT_TIME;
		$args['ct_update'] = CURRENT_TIME;

		if($id = $this->data->add($args)){
			//添加tag信息
			$this->addTags($id,$tags);
			//更新封面信息
			if($_FILES['cover'] && !$_FILES['cover']['error']){
				$para['id'] = $id;
				$rs = cartoon_util_upload::upload_cover('cover',$id);
				if($rs['result']){
					$para['ct_cover'] = $rs['url'];
					$this->data->modifyData($para,'cartoon_cartoon');
				}
			}
			cartoon_util_interface::cacheGetRecomCT(CONST_CT::RECOM_NEW,true );
			
		}
 		$this->_redirect($this->app,$this->controller,'cartoon_list');
	}
	
	/**
	 * 修改manhua 
	 *
	 */
	public function edit_action(){
		$args['id'] = _('id');
		_('cartoon_name') && $args['ct_title'] = _('cartoon_name');
		_('alias_name') && $args['ct_alias_name'] = _('alias_name');
		_('real_name') && $args['ct_real_name'] = _('real_name');
		_('selected_author') && $args['ct_author'] = _('selected_author');
		$args['ct_flag'] = _('status',1);
		_('cartoon_name') && $args['ct_char'] = cartoon_util_interface::getfirstchar(_('cartoon_name'));
		_('cartoon_py') && $args['ct_py'] = _('cartoon_py');
		_('zone') && $args['ct_nation'] = _('zone')?_('zone'):'1';
		_('language') && $args['ct_lang'] = _('language')?_('language'):'1';
		_('selected_series') && $args['ct_series'] = _('selected_series');
		_('s_desc') && $args['ct_simple_desc'] = _('s_desc');
		$args['ct_reader_group']	=	_('readerGroup');
		_('_cartoon_desc') && $args['ct_desc'] = _('_cartoon_desc');
		$args['ct_category'] = _('selected_cate');
		$args['ct_update'] = CURRENT_TIME;
		
		$tags = $this->parseTag(_('tags'));
		
		/**
		 * 处理tag信息
		 */		
		$ctinfo = $this->data->getRow($args['id']);
		if($ctinfo['ct_tags'] != $tags){
			//如果有不一样,则重新添加tag信息
			$this->tag->removeRel($args['id']);
			//添加tag
			$this->addTags($args['id'],$tags);
			
			$args['ct_tags'] = $tags;
		}
		
		if($_FILES['cover'] && !$_FILES['cover']['error']){
			$rs = cartoon_util_upload::upload_cover('cover',$args['id']);
			if($rs['result']){
				$args['ct_cover'] = $rs['url'];
			}
		}
		if($this->data->modifyData($args,'cartoon_cartoon')){
			$cartoon = $this->data->getRow($args['id']);
			if($cartoon['ct_recoment']>0){//更新缓存
				cartoon_util_interface::cacheGetRecomCT($cartoon['ct_recoment'],true);
			}
		}
		$this->_redirect($this->app,$this->controller,'cartoon_list');
	}
	
	private function parseTag($tags){
		if($tags){
			$tags = str_replace("，", ",", $tags);
			$tags = str_replace(" ", ",", $tags);
			$tags = str_replace("、", ",", $tags);
			$tags = str_replace(";", ",", $tags);
		}
		return $tags;
	}
	
	private function addTags($cartoon_id,$tags){
		if(!($cartoon_id && $tags)) return false;
		$tags = explode(',',$tags);
		foreach ($tags as $tag){
			//添加tag信息
			$tid = $this->tag->addTag(trimAll($tag));
			//添加关系
			$this->tag->addRel($tid,$cartoon_id);
		}
		return ;
	}
	
	/**
	 * 列表
	 *
	 */
	public function cartoon_list_action(){
		$this->requireLogin();
		$page = _('page',1);
		
		$flag	=	_('flag');
		$args = array();
		$args['uid'] = self::$login['uid'];
		if($flag || $flag===0){
			$args['ct_recoment'] = $flag;
		}
		/**
		 * 作者列表
		 */
//		$authors = cartoon_util_interface::cacheGetAuthors();
		
		$list = $this->data->getCartoonList($args,$page,CT_PAGE_SIZE);
		
		$page = $this->genPages($list['INFO']);
		
		$this->ret = array('data'=>$list['ROWS'],'pages'=>$page);
		
		$this->view = 'cartoon_list';
	}
	
	public function chapter_list_action(){
		
		$page = _('page',1);
		
		$cartoon_id = _('ctid');
		
		$this->requireLogin();
		
		$cartoon_info = $this->data->getRow($cartoon_id);
		
		$list = $this->chapter->getChapterListPages($cartoon_id,$page,15);
		
		$pages = $this->genPages($list['INFO'],array('cartoon_id'=>$cartoon_id));
		
		$this->ret = array('data'=>$list['ROWS'],'pages'=>$pages,'cartoon_info'=>$cartoon_info);
//		var_dump($this->ret);
		$this->view = 'chapter_list';

	}
	
	public function batch_photo_form_action(){
		$chapter_id = _('chapter_id');
		$page = _('page',1);
		/**
		 * 获取章节信息
		 * @var unknown_type
		 */
		$chapter_info = $this->chapter->getChapterRow($chapter_id);
		/**
		 * 获取图片列表
		 * @var unknown_type
		 */
		$chapter = $this->photo->getPhotoListPage($chapter_id,$page,CT_PAGE_SIZE);
		
		$pages = $this->genPages($chapter['INFO'],array('chapter_id'=>$chapter_id));
		
		$this->ret = array('data'=>$chapter['ROWS'],'pages' => $pages, 'chapterinfo'=>$chapter_info);
		
		$this->view = 'batch_upload';
	}
	
	/**
	 * 添加章节信息
	 */
	public function add_chapter_action(){
		$this->_renderJson();
		$para['cartoon_id'] = _('cartoon_id');
		$para['chapter_title'] = _('title');
		$para['info']	=	_('desc');
		$para['order_num'] = _('order',0);
		$para['add_time'] = time();
		$para['id'] = _('id');
		if($para['id']){
			if($this->chapter->edit_chapter($para)){
				$this->ret = self::success();
			}else $this->ret = self::failure('修改失败');
		}
		else {
			if($rs = $this->chapter->add_chapter($para)){
				$this->ret = self::success($rs);
			}
			else{
				$this->ret = self::failure('添加失败');
			}
		}
	}
	
	/**
	 * 修改章节信息
	 *
	 */
	public function edit_chapter_action(){
		$this->_renderJson();
		$para['id'] = _('id');
		_('cartoon_id') && $para['cartoon_id'] = _('cartoon_id');
		_('title') && $para['chapter_title'] = _('title');
		_('desc') && $para['info']	=	_('desc');
		_('order',0) && $para['order_num'] = _('order',0);
		$para['add_time'] = time();
		if($rs = $this->chapter->edit_chapter($para)){
			$this->ret = self::success($rs);
		}else{
			$this->ret = self::failure('修改失败');
		}
	}
	
	/**
	 * 删除章节信息
	 */
	public function remove_chapter_action(){
		$this->_renderJson();
		$chapter_id  = _('id');
		if($rs = $this->chapter->del_chapter($chapter_id)){
			$this->ret = self::success();
		}else{
			$this->ret = self::failure('添加失败');
		}
	}
	
	/**
	 * 上传漫画图片
	 */
	public function send_batch_photo_action(){
		
		$args['img_cartoon_id'] = _('cartoon_id');
		$args['img_chapter_id'] = _('chapter_id');
		
		if(!($args['img_cartoon_id'] && $args['img_chapter_id'])){
			cartoon_util_interface::exit_err('参数错误');
		}
		
		$file = 'Filedata';
		$args['img_title'] = $name = $_FILES[$file]['name'];
		if(strpos($name,'.',strlen($name)-5)!==false){
			$args['img_title'] = substr($name,0,strpos($name,'.',strlen($name)-5));
		}
//		$args['img_title'] = $_FILES[$file]['name'];
		$args['img_filesize'] = $_FILES[$file]['size'];
		cartoon_util_upload::setCuteConf($_SERVER['upload_size']);
		$rs = cartoon_util_upload::upload_photo($file,$args['img_chapter_id']);
		if($rs['result']){
			$args['img_url'] = $rs['url'];
			$args['img_time'] = CURRENT_TIME;
			$this->photo->add_photo($args);
			
		}else{
			if (isset($_FILES["Filedata"]["error"])) {
				$msg = $_FILES["Filedata"]["error"];
			}else{
				$msg = '上错失败';
			}
			cartoon_util_interface::exit_err($msg);
		}
		exit(0);
		$this->ret = self::success();

		$this->_renderJson();
	}
	
	/**
	 * 修改图片标题
	 */
	public function edit_img_info_action(){
		$args['id'] = _('id');
		_('title') && $args['img_title'] = _('title');
		_('order') && $args['img_order'] = _('order');
		$this->_renderJson();
		if($args['id'] && ($args['img_title'] || $args['img_order'])){
			if($this->photo->modifyData($args,'cartoon_images')){
				$this->ret = self::success();
			}
			else{
				$this->ret = self::failure('修改失败！');
			}
		}else{
			$this->ret = self::failure('参数错误!');
		}
	}
	
	/**
	 * 删除图片
	 */
	
	public function delImg_action(){
		$id = _('id');
		$this->_renderJson();
		if($this->photo->del_photo($id)){
			$this->ret = self::success();
		}else{
			$this->ret = self::failure('删除失败!');
		}
	}
	
	/**
	 * 推荐漫画
	 *
	 */
	public function recommCartoon_action(){
		$cartoon_ids	=	_('catoon_id');
		$flag			=	_('flag',0);
		$this->_renderJson();
		if($this->data->recomCartoon($cartoon_ids,$flag)){
			$this->ret = self::success();
		}else{
			$this->ret = self::failure('推荐失败');
		}
	}
	
	/**
	 * 我的漫画列表
	 *
	 */
	public function my_action(){
		$this->requireLogin();
		$uid = self::$login['uid'];
		$page = _('page',1);
		$pageSize = 16;
		//获取我的漫画列表
		$cartoonList = Model::factory('cartoon_model_data')->getCartoonList(array('uid'=>$uid),$page,$pageSize);
//		print_r($cartoonList);
		$this->ret = array('data'=>$cartoonList['ROWS'],'pages'=>$this->genPages($cartoonList['INFO']));
		
		$this->view = 'myctlist';
	}
	
	/**
	 * 我订阅的漫画列表
	 *
	 */
	public function mysub_action(){
		$this->requireLogin();
		$uid = self::$login['uid'];
		$page = _('page',1);
		$pageSize = 16;
		//获取我的漫画列表
		$cartoonList = Model::factory('cartoon_model_data')->getSubList($uid,$page,$pageSize);
//		print_r($cartoonList);
		$this->ret = array('data'=>$cartoonList['ROWS'],'pages'=>$this->genPages($cartoonList['INFO']));
		
		$this->view = 'myctlist';
	}
	
	/**
	 * 我浏览的漫画列表
	 *
	 */
	public function myview_action(){
		$this->requireLogin();
		$uid = self::$login['uid'];
		$page = _('page',1);
		$pageSize = 16;
		//获取我的漫画列表
		$cartoonList = Model::factory('cartoon_model_data')->getViewList($uid,$page,$pageSize);
//		print_r($cartoonList);
		$this->ret = array('data'=>$cartoonList['ROWS'],'pages'=>$this->genPages($cartoonList['INFO']));
		
		$this->view = 'myctlist';
	}
	
	/**
	 * 我订阅的漫画列表
	 *
	 */
	public function mycollect_action(){
		$this->requireLogin();
		$uid = self::$login['uid'];
		$page = _('page',1);
		$pageSize = 16;
		//获取我的漫画列表
		$cartoonList = Model::factory('cartoon_model_data')->getCollectList($uid,$page,$pageSize);
//		print_r($cartoonList);
		$this->ret = array('data'=>$cartoonList['ROWS'],'pages'=>$this->genPages($cartoonList['INFO']));
		
		$this->view = 'myctlist';
	}
	
	/**
	 * 我订阅的漫画列表
	 *
	 */
	public function myscore_action(){
		$this->requireLogin();
		$uid = self::$login['uid'];
		$page = _('page',1);
		$pageSize = 16;
		//获取我的漫画列表
		$cartoonList = Model::factory('cartoon_model_data')->getscoreListPage($uid,$page,$pageSize);
//		print_r($cartoonList);
		$this->ret = array('data'=>$cartoonList['ROWS'],'pages'=>$this->genPages($cartoonList['INFO']));
		
		$this->view = 'myctlist';
	}
	
	/**
	 * 添加分类信息
	 *
	 */
	public function addCate_action(){
		$this->_renderJson();
		$cateTitle = _('cate_title');
		$cate_order = _('cate_order');
		$cateid	= (int)_('cateid');
		$recomm	=	_('recomm')?_('recomm'):1;
		if($cateid>0)	{
			if($this->data->editCate(array('id'=>$cateid,'cate_name'=>$cateTitle,'cate_order'=>$cate_order,'cate_flag'=>$recomm))){
				$this->ret = self::success();
				cartoon_util_interface::cacheGetCateList(true);
			}
			else $this->ret = self::failure('修改失败!');
		}
		elseif($rs = $this->data->addCate($cateTitle,$cate_order)){
			if($rs == ERR_EXISTS){
				$this->ret	=	self::failure('分类已经存在!');
			}else{
				cartoon_util_interface::cacheGetCateList(true);
				$this->ret = self::success();
			}
		}else $this->ret = self::failure('添加失败');
	}
	
	/**
	 * 删除分类
	 *
	 */
	public function delCate_action(){
		$this->_renderJson();
		$id = _('id');
		if($id){
			if($this->data->delCate($id)){
				cartoon_util_interface::cacheGetCateList(true);
				$this->ret = self::success();
			}else{
				$this->ret = self::failure('删除失败！');
			}
		}else{
			$this->ret = self::failure('参数错误!');
		}
	}
	/**
	 * 分类列表
	 */
	public function category_list_action(){
		$cateList = cartoon_util_interface::cacheGetCateList();
		$this->ret = array('data'=>$cateList);
		$this->view = 'categoy_list';
	}
	/**
	 * 添加作者
	 *
	 */
	public function add_author_action(){
		$args['name']	=	_('author');
		$args['nation']	=	_('nation');
		$this->_renderJson();
		if(_('id')){
			$args['id'] = _('id');
			if($this->data->edit_author($args)){
				$this->ret = self::success();
			}
			else $this->ret = self::failure('修改失败');
		}else{
			if($rs = $this->data->add_author($args)){
				if($rs == ERR_EXISTS){
					$this->ret = self::failure('作者名已经存在！');
				}else{
					$this->ret = self::success();
				}
			}else{
				$this->ret = self::failure('添加失败，请稍候再试!');
			}
		}
	}
	
	/**
	 * 作者列表
	 *
	 */
	public function author_list_action(){
		$args['name'] = _('name');
		$args['nation'] = _('nation');
		$page = _('page');
		$rs = $this->data->getAuthorListPages($args,$page,15);
		
		$pages = $this->genPages($rs['INFO'],array('name'=>_('name'),'nation'=>_('nation')));
		
		$this->ret = array('data'=>$rs['ROWS'],'pages' =>$pages);
		
		$this->view = 'author_list';
	}
	
	public function js_author_list_action(){
		$authorList = cartoon_util_interface::cacheGetAuthors();
		$js = '[';
		$sep = '';
		foreach ((array) $authorList as $row){
			$js.=$sep.'"'.$row['name'].'"';
			$sep = ',';
		}
		$js.=']';
		echo $js;
		exit;
	}
}
?>
